import glob
import os.path
import sys
from pathlib import Path
from tqdm import tqdm
from pikepdf import Pdf

par_path = Path(__file__).parent


def validate_pdf(file):
    try:
        with Pdf.open(file) as f:
            return True
    except:
        return False


def create_or_append(file, row):
    with open(file, "a+", encoding='utf-8') as fw:
        fw.write(row + os.linesep)


def read_all(file):
    if os.path.isfile(file):
        with open(file, 'r', encoding='utf8', errors='ignore') as f:
            return set([r.strip() for r in f.readlines()])
    else:
        with open(file, "w", encoding='utf-8') as fw:
            pass
        return {}


def all_pdfs_validate(pdfs_dir):
    valid_file = os.path.join(pdfs_dir, 'valid.txt')
    invalid_file = os.path.join(pdfs_dir, 'invalid.txt')
    all_valids = read_all(valid_file)
    all_invalids = read_all(invalid_file)
    all_files = glob.glob(rf"{pdfs_dir}\**\*.pdf", recursive=True)
    for pdf_file in tqdm(all_files):
        if pdf_file in all_valids or pdf_file in all_invalids:
            continue
        if validate_pdf(pdf_file):
            create_or_append(valid_file, pdf_file)
            # sys.stdout.write('.')
            # sys.stdout.flush()
        else:
            create_or_append(invalid_file, pdf_file)
            # print(pdf_file)


if __name__ == '__main__':
    all_pdfs_validate(r"C:\Users\yang.lei\Downloads")
